
package gui;

import gui.Jd_add_proveedor;
import gui.Jd_modificar_proveedor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import modelo.TableModelProveedores;
import persistencia.ProveedoresDAO;
import pojos.Proveedor;

/**
 *
 * @author alejandrolopezdelolmo
 */
public class JpConsultarProveedores extends javax.swing.JPanel {
    VentanaPrincipal vp;
    ArrayList<Proveedor> alProveedores;
    /**
     * Creates new form PanelSearchBook
     */
    
    public JpConsultarProveedores(VentanaPrincipal vp) {
        this.vp=vp;
        initComponents();
        
   
        //agrupo los radio button
        ButtonGroup bg=new ButtonGroup();
        bg.add(this.jcbNombreProveedor);
        bg.add(this.jcbCodProveedor);
        bg.add(this.jcbPoblacion);
        
        //deshabilito el resto de textfield
        jcbNombreProveedor.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfNombreProveedor.setEnabled(true);
                jtfCodProveedor.setText("");
                jtfCodProveedor.setEnabled(false);
                jtfPoblacionProveedor.setText("");
                jtfPoblacionProveedor.setEnabled(false);
                
            }
        });
        jcbCodProveedor.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfCodProveedor.setEnabled(true);
                jtfNombreProveedor.setText("");
                jtfNombreProveedor.setEnabled(false);
                jtfPoblacionProveedor.setText("");
                jtfPoblacionProveedor.setEnabled(false);
            }
        });
        jcbPoblacion.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                jtfPoblacionProveedor.setEnabled(true);
                jtfCodProveedor.setText("");
                jtfCodProveedor.setEnabled(false);
                jtfNombreProveedor.setText("");
                jtfNombreProveedor.setEnabled(false);
            }
        });
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableProveedores = new javax.swing.JTable();
        jPfiltros = new javax.swing.JPanel();
        jcbCodProveedor = new javax.swing.JCheckBox();
        jtfCodProveedor = new javax.swing.JTextField();
        jcbNombreProveedor = new javax.swing.JCheckBox();
        jtfNombreProveedor = new javax.swing.JTextField();
        jcbPoblacion = new javax.swing.JCheckBox();
        jtfPoblacionProveedor = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jbConsultarProveedor = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        jbAltaProveedor = new javax.swing.JButton();
        jbModificarProveedor = new javax.swing.JButton();
        jbEliminarProveedor = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(800, 600));
        setLayout(new java.awt.BorderLayout());

        jPanel3.setMaximumSize(new java.awt.Dimension(800, 600));
        jPanel3.setPreferredSize(new java.awt.Dimension(800, 600));

        jPanel4.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jTableProveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Cod Proveedor", "Nombre", "CIF", "Direccion", "Cod Postal", "Poblacion", "Pais"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableProveedores.setMaximumSize(new java.awt.Dimension(800, 800));
        jScrollPane2.setViewportView(jTableProveedores);

        jPanel4.add(jScrollPane2);

        jPfiltros.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPfiltros.setLayout(new java.awt.GridLayout(4, 2));

        jcbCodProveedor.setText("Codigo Proveedor");
        jPfiltros.add(jcbCodProveedor);
        jPfiltros.add(jtfCodProveedor);

        jcbNombreProveedor.setText("Nombre");
        jPfiltros.add(jcbNombreProveedor);
        jPfiltros.add(jtfNombreProveedor);

        jcbPoblacion.setText("Poblacion");
        jPfiltros.add(jcbPoblacion);
        jPfiltros.add(jtfPoblacionProveedor);
        jPfiltros.add(jLabel1);

        jbConsultarProveedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_find.png"))); // NOI18N
        jbConsultarProveedor.setFocusable(false);
        jbConsultarProveedor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbConsultarProveedor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbConsultarProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConsultarProveedorActionPerformed(evt);
            }
        });
        jPfiltros.add(jbConsultarProveedor);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(50, 45));

        jbAltaProveedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_plus.png"))); // NOI18N
        jbAltaProveedor.setFocusable(false);
        jbAltaProveedor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbAltaProveedor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbAltaProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAltaProveedorActionPerformed(evt);
            }
        });
        jToolBar1.add(jbAltaProveedor);

        jbModificarProveedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_edit1.png"))); // NOI18N
        jbModificarProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbModificarProveedorActionPerformed(evt);
            }
        });
        jToolBar1.add(jbModificarProveedor);

        jbEliminarProveedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_cancel.png"))); // NOI18N
        jbEliminarProveedor.setFocusable(false);
        jbEliminarProveedor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEliminarProveedor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbEliminarProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEliminarProveedorActionPerformed(evt);
            }
        });
        jToolBar1.add(jbEliminarProveedor);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(135, 135, 135)
                                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );

        add(jPanel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jbEliminarProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEliminarProveedorActionPerformed
        ProveedoresDAO provDao=new ProveedoresDAO();
        try{
            int op=JOptionPane.showConfirmDialog(this, "Estas seguro de dar de baja a"+
                alProveedores.get(jTableProveedores.getSelectedRow()).getNombreProveedor() , "Confirmar Baja",JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
            if(op==JOptionPane.OK_OPTION){
                 provDao.eliminaProveedor(alProveedores.get(jTableProveedores.getSelectedRow()));
                JOptionPane.showMessageDialog(this, "Se ha dado de baja correctamente", "Baja correcta",JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(Exception ex){
           JOptionPane.showMessageDialog(this, "Debes seleccionar un proveedor de la tabla", "No hay seleccion",JOptionPane.WARNING_MESSAGE);
       }
    }//GEN-LAST:event_jbEliminarProveedorActionPerformed

    private void jbConsultarProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConsultarProveedorActionPerformed
        if(jcbCodProveedor.isSelected()||jcbNombreProveedor.isSelected()||jcbPoblacion.isSelected()){
            alProveedores=new ArrayList();
            if(jcbCodProveedor.isSelected()){
                if(jtfCodProveedor.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Debes introducir datos en el campo de texto", "NO HAY DATOS",JOptionPane.INFORMATION_MESSAGE);
                }
                else{
                    try{
                        ProveedoresDAO provDao=new ProveedoresDAO();

                        alProveedores=(ArrayList)provDao.getProveedor(Integer.parseInt(jtfCodProveedor.getText()));
                    }
                    catch(NumberFormatException ex){
                        JOptionPane.showMessageDialog(this, "Debes introducir datos numericos en el campo de texto", "ERROR DE DATOS",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }  
            if(jcbNombreProveedor.isSelected()){
                try{
                    ProveedoresDAO provDao=new ProveedoresDAO();

                    alProveedores=(ArrayList)provDao.getProveedorXnombre(jtfNombreProveedor.getText());
                }
                catch(NumberFormatException ex){
                    JOptionPane.showMessageDialog(this, "Debes introducir datos numericos en el campo de texto", "ERROR DE DATOS",JOptionPane.WARNING_MESSAGE);
                }
            }
            if(jcbPoblacion.isSelected()){
                try{
                    ProveedoresDAO provDao=new ProveedoresDAO();

                    alProveedores=(ArrayList)provDao.getProveedoresXpoblacion(jtfPoblacionProveedor.getText());
                }
                catch(NumberFormatException ex){
                    JOptionPane.showMessageDialog(this, "Debes introducir datos numericos en el campo de texto", "ERROR DE DATOS",JOptionPane.WARNING_MESSAGE);
                }
            }
            if(!alProveedores.isEmpty()){
                //creo TableModel
                System.out.println(""+alProveedores.get(0).getCodProveedor());
                TableModelProveedores tmp = new TableModelProveedores(alProveedores);
                jTableProveedores.setModel(tmp);
                tmp.fireTableDataChanged();//actualizo la tabla
            }
            else{
                JOptionPane.showMessageDialog(this, "No hay ningun proveedor por esa consulta", "NO HAY DATOS",JOptionPane.WARNING_MESSAGE);
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Debes elegir algun filtro de busqueda", "ERROR DE CONSULTA",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jbConsultarProveedorActionPerformed

    private void jbAltaProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAltaProveedorActionPerformed
        Jd_add_proveedor ep = new Jd_add_proveedor(this);
    }//GEN-LAST:event_jbAltaProveedorActionPerformed

    private void jbModificarProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbModificarProveedorActionPerformed
       try{
            Jd_modificar_proveedor ep = new Jd_modificar_proveedor(this,alProveedores.get(jTableProveedores.getSelectedRow()));
       }catch(Exception ex){
           JOptionPane.showMessageDialog(this, "Debes seleccionar un proveedor de la tabla", "No hay seleccion",JOptionPane.WARNING_MESSAGE);
       }
    }//GEN-LAST:event_jbModificarProveedorActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPfiltros;
    private javax.swing.JScrollPane jScrollPane2;
    public javax.swing.JTable jTableProveedores;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbAltaProveedor;
    private javax.swing.JButton jbConsultarProveedor;
    private javax.swing.JButton jbEliminarProveedor;
    private javax.swing.JButton jbModificarProveedor;
    private javax.swing.JCheckBox jcbCodProveedor;
    private javax.swing.JCheckBox jcbNombreProveedor;
    private javax.swing.JCheckBox jcbPoblacion;
    private javax.swing.JTextField jtfCodProveedor;
    private javax.swing.JTextField jtfNombreProveedor;
    private javax.swing.JTextField jtfPoblacionProveedor;
    // End of variables declaration//GEN-END:variables
}
